package com.ruoyi.module.test;

import cn.hutool.core.thread.ThreadUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

/**
 * @author: xiaYZ 2023/2/16
 * @version: 1.0
 */
public class HuToolThreadDemo {


    volatile int x = 16;

    int y = 16;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public static void main(String[] args) throws InterruptedException {
        HuToolThreadDemo huToolThreadDemo = new HuToolThreadDemo();
        //5个线程
        CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(3);
        ArrayList<Integer> list = new ArrayList<>();
        long l = System.currentTimeMillis();
        ThreadUtil.execute(() -> {

            list.add(1);


            countDownLatch.countDown();
        });
        ThreadUtil.execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            list.add(2);
            huToolThreadDemo.setX(huToolThreadDemo.getX() + 1);
            System.out.println("x+1的的值:" + huToolThreadDemo.getX());
            huToolThreadDemo.setY(huToolThreadDemo.getY() + 1);
            System.out.println("y+1的的值:" + huToolThreadDemo.getY());
            countDownLatch.countDown();
        });
        ThreadUtil.execute(() -> {
            list.add(3);
            huToolThreadDemo.setX(10);
            System.out.println("x+10的的值:" + huToolThreadDemo.getX());
            huToolThreadDemo.setY(10);
            System.out.println("y+10的的值:" + huToolThreadDemo.getY());
            countDownLatch.countDown();
        });

        //等待所有线程执行完毕
        countDownLatch.await();
        long l1 = System.currentTimeMillis();
        System.out.println(l1 - l);
        for (Integer integer : list) {
            System.out.println(integer);
        }
        // ThreadUtil.shutdown();

    }

    public List<Integer> way1() throws InterruptedException {
        Thread.sleep(1000);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        return list;
    }

    public List<Integer> way2() throws  InterruptedException {
        Thread.sleep(1000);
        ArrayList<Integer> list = new ArrayList<>();
        list.add(2);
        Set<Integer> hashSet = new HashSet<>();
        return list;
    }

    public List<Integer> way3() {
        List<Integer> list = new ArrayList<>();
        list.add(3);
        throw new RuntimeException("测试异常");
        // return list;
    }


}

